<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="初步接洽" prop="lPrecontact">
      <el-input v-model="dataForm.lPrecontact" placeholder="初步接洽"></el-input>
    </el-form-item>
    <el-form-item label="需求确定" prop="lRequire">
      <el-input v-model="dataForm.lRequire" placeholder="需求确定"></el-input>
    </el-form-item>
    <el-form-item label="方案/报价" prop="lProgramme">
      <el-input v-model="dataForm.lProgramme" placeholder="方案/报价"></el-input>
    </el-form-item>
    <el-form-item label="谈判/合同" prop="lNegotiation">
      <el-input v-model="dataForm.lNegotiation" placeholder="谈判/合同"></el-input>
    </el-form-item>
    <el-form-item label="赢单数量" prop="lWinordernum">
      <el-input v-model="dataForm.lWinordernum" placeholder="赢单数量"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          lMonth: 0,
          lPrecontact: '',
          lRequire: '',
          lProgramme: '',
          lNegotiation: '',
          lWinordernum: ''
        },
        dataRule: {
          lPrecontact: [
            { required: true, message: '初步接洽不能为空', trigger: 'blur' }
          ],
          lRequire: [
            { required: true, message: '需求确定不能为空', trigger: 'blur' }
          ],
          lProgramme: [
            { required: true, message: '方案/报价不能为空', trigger: 'blur' }
          ],
          lNegotiation: [
            { required: true, message: '谈判/合同不能为空', trigger: 'blur' }
          ],
          lWinordernum: [
            { required: true, message: '赢单数量不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.lMonth = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.lMonth) {
            this.$http({
              url: this.$http.adornUrl(`/com/salesforecast/info/${this.dataForm.lMonth}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.lPrecontact = data.salesforecast.lPrecontact
                this.dataForm.lRequire = data.salesforecast.lRequire
                this.dataForm.lProgramme = data.salesforecast.lProgramme
                this.dataForm.lNegotiation = data.salesforecast.lNegotiation
                this.dataForm.lWinordernum = data.salesforecast.lWinordernum
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/com/salesforecast/${!this.dataForm.lMonth ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'lMonth': this.dataForm.lMonth || undefined,
                'lPrecontact': this.dataForm.lPrecontact,
                'lRequire': this.dataForm.lRequire,
                'lProgramme': this.dataForm.lProgramme,
                'lNegotiation': this.dataForm.lNegotiation,
                'lWinordernum': this.dataForm.lWinordernum
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
